package com.atmc.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.Rule;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.atmc.entity.ResponseResult;
import com.atmc.entity.User;
import com.atmc.service.FilesService;
import com.atmc.feign.GlobalFeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
@Slf4j
public class MineController {
    public static final String RESOURCE_NAME1="helloWorld";
    public static final String RESOURCE_NAME2="consumer_test";//loadfile
    public static final String RESOURCE_NAME3="loadfile";//loadfile
    @Autowired
    GlobalFeignService globalFeignService;
    @Autowired
    FilesService filesService;

    @RequestMapping("/info")
    public String hello(){
        return "hello SpringSecurity.  chuyings";
    }
    @PostMapping("/upfile")
    public ResponseResult upfiles(@RequestPart("photos") MultipartFile[] photos) throws IOException {
        for (MultipartFile photo : photos) {
            String originalFilename = photo.getOriginalFilename();
            FileInputStream fis = (FileInputStream) photo.getInputStream();
        }
        return new ResponseResult(200,"上传成功");
    }
    @PostMapping("/loadfile")
    @SentinelResource(value = RESOURCE_NAME3,blockHandler = "blockHandlerForDegrade")
    public ResponseResult loadfile(){

        return new ResponseResult(200,"nihao");
    }
    public ResponseResult blockHandlerForDegrade(BlockException ex){

        return new ResponseResult(200,"你被限制访问了。");
    }

    @PostMapping("/consumer/user/{userName}")
    public User testFeign(@PathVariable("userName")String userName){
        return globalFeignService.selectOne(userName);
    }

    @RequestMapping(value = "consumer/userById/{user_id}",method = RequestMethod.GET)
    public User selectUserById(@PathVariable("user_id")Long user_id){
        return globalFeignService.userselectById(user_id);
    }

    @PostMapping("helloWorld")
    @SentinelResource(value = RESOURCE_NAME1,blockHandler = "blockHandlerForGetUser")
    public ResponseResult helloworld(){

        return new ResponseResult(200,"执行正常");
    }

    public  ResponseResult blockHandlerForGetUser(BlockException ex){

        return new ResponseResult(500,"流控限制,稍后访问");
    }
    @GetMapping("/consumer/test")
    @SentinelResource(value = RESOURCE_NAME2,blockHandler = "blockHandlerForGetUser")
    public ResponseResult test1(){
        return globalFeignService.test();
    }
    //当前类注入spring容器后，就会自动加载该方法，配置流控规则
    @PostConstruct
    public void initFlowRules(){
        List<FlowRule> rules=new ArrayList<>();
        FlowRule flowRule=new FlowRule();

        //设置资源名
        flowRule.setResource(RESOURCE_NAME1);
        //设置流控规则
        flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        //设置受保护的资源阈值 1s一次
        flowRule.setCount(1);
        rules.add(flowRule);

        FlowRule flowRule1=new FlowRule();
        flowRule1.setResource(RESOURCE_NAME2);
        flowRule1.setGrade(RuleConstant.FLOW_GRADE_QPS);
        flowRule.setCount(1);
//        flowRule.setStrategy()
        rules.add(flowRule);
        //加载流控规则配置
        FlowRuleManager.loadRules(rules);
    }
    @PostConstruct
    public void initDegradeRule(){
        List<DegradeRule> degradeRules=new ArrayList<>();
        //声明一个降级规则的对象
        DegradeRule degradeRule1=new DegradeRule();
        degradeRule1.setResource(RESOURCE_NAME3);
        // set threshold RT, 100 ms
        degradeRule1.setCount(1);
        //设置慢调用比例阈值
        degradeRule1.setSlowRatioThreshold(0.1);
        //设置最小请求数
        degradeRule1.setMinRequestAmount(5);
        //设置恢复时间窗口10s
        degradeRule1.setGrade(RuleConstant.DEGRADE_GRADE_RT);
        degradeRule1.setTimeWindow(5);
        degradeRules.add(degradeRule1);
//        degradeRule1.setGrade(RuleConstant.DEFAULT_WINDOW_INTERVAL_MS);
        DegradeRuleManager.loadRules(degradeRules);

    }


}